﻿using AutoMapper;
using DataServer.ApplicationLib.Entities;
using DataServer.ApplicationLib.Repositories;
using DataServer.CommonLib.Models.Dimension;
using Swimj.CommonLib.Base;
using Swimj.DataCore;

namespace DataServer.ApplicationLib.Services
{
    public class DimensionService : IDimensionService
    {
        private readonly IDimensionRepository _dimensionRepository;
        private readonly IDimensionValuesRepository _dimensionValuesRepository;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IMapper _mapper;

        public DimensionService(IDimensionRepository dimensionRepository,
            IDimensionValuesRepository dimensionValuesRepository,
            IUnitOfWork unitOfWork,
            IMapper mapper)
        {
            _dimensionRepository = dimensionRepository;
            _dimensionValuesRepository = dimensionValuesRepository;
            _unitOfWork = unitOfWork;
            _mapper = mapper;
        }


        public async Task<PaginatedResult<DimensionInfoResponseDto>> QueryDimensionsByPageAsync(
            DimensionSearchRequestDto? model)
        {
            var conditionCollection = new ConditionCollection();
            if (model != null)
            {
                var result =
                    await _dimensionRepository.QueryFilterByPageAsync(conditionCollection, model.Page, model.Limit);

                return await PaginatedResult<DimensionInfoResponseDto>.SuccessAsync(
                    _mapper.Map<List<DimensionInfoResponseDto>>(result.Item1),
                    result.Item2,
                    model.Page, model.Limit);
            }

            return await PaginatedResult<DimensionInfoResponseDto>.SuccessAsync(new List<DimensionInfoResponseDto>(),
                0, 0, 0);
        }

        public Task<Result<IList<DimensionInfoResponseDto>>> QueryDimensionsAsync(DimensionSearchRequestDto? model)
        {
            throw new NotImplementedException();
        }

        public async Task<Result<DimensionInfoResponseDto>> CreateDimensionAsync(DimensionEditRequestDto model)
        {
            var dimensionEntity = _mapper.Map<DimensionEntity>(model);

            await _dimensionRepository.InsertAsync(dimensionEntity);

            if (model.Values != null)
            {
                var dimensionValuesEntities = _mapper.Map<IList<DimensionValuesEntity>>(model.Values);
                await _dimensionValuesRepository.BatchInsertAsync(dimensionValuesEntities);
            }

            _unitOfWork.Commit();
            return await Result<DimensionInfoResponseDto>.SuccessAsync(
                _mapper.Map<DimensionInfoResponseDto>(dimensionEntity));
        }

        public async Task<Result<DimensionInfoResponseDto>> GetDimensionAsync(Guid id)
        {
            var dimensionEntity = await _dimensionRepository.GetAsync(id);
            return await Result<DimensionInfoResponseDto>.SuccessAsync(
                _mapper.Map<DimensionInfoResponseDto>(dimensionEntity));
        }

        public Task<Result<DimensionInfoResponseDto>> UpdateDimensionAsync(Guid id, DimensionEditRequestDto model)
        {
            throw new NotImplementedException();
        }

        public Task<object?> TestDimensionAsync(string dimensionCode, int limit)
        {
            throw new NotImplementedException();
        }
    }
}